package tr.edu.metu.is.mega.server.business;

import java.util.ArrayList;
import java.util.List;

import tr.edu.metu.is.mega.common.dto.ActivityTemplateDTO;
import tr.edu.metu.is.mega.common.dto.ActivityType;
import tr.edu.metu.is.mega.common.dto.ScheduleTemplateDTO;
import tr.edu.metu.is.mega.server.core.AbstractDTOHelper;
import tr.edu.metu.is.mega.server.entity.Activitytemplate;
import tr.edu.metu.is.mega.server.entity.Scheduletemplate;

/**
 * Helper class to convert between DTO and entity.
 */
public class ScheduleTemplateDTOHelper extends AbstractDTOHelper<ScheduleTemplateDTO, Scheduletemplate> {

    /**
     * {@inheritDoc}
     */
    @Override
    public ScheduleTemplateDTO convertToDTO(Scheduletemplate entity) {
        ScheduleTemplateDTO dto = new ScheduleTemplateDTO();
        dto.setPkid(entity.getPkid());
        dto.setShortname(entity.getShortname());
        dto.setDescription(entity.getDescription());

        List<ActivityTemplateDTO> activities = new ArrayList<ActivityTemplateDTO>(entity.getActivities().size());

        for (Activitytemplate activitytemplate : entity.getActivities()) {
            ActivityTemplateDTO templateDTO = new ActivityTemplateDTO();
            templateDTO.setPkid(activitytemplate.getPkid());
            templateDTO.setSequenceNo(activitytemplate.getSequenceno());
            templateDTO.setDueDate(activitytemplate.getDuedate());
            templateDTO.setDueTime(activitytemplate.getDuetime());
            templateDTO.setAlertTime(activitytemplate.getAlerttime());
            templateDTO.setSubmissionType(ActivityType.findByCode(activitytemplate.getSubmissiontype()));
            templateDTO.setDescription(activitytemplate.getDescription());
            activities.add(templateDTO);
        }

        dto.setActivities(activities);

        return dto;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void convertToEntity(ScheduleTemplateDTO dto, Scheduletemplate entity) {
        entity.setPkid(dto.getPkid());
        entity.setShortname(dto.getShortname());
        entity.setDescription(dto.getDescription());

        List<Activitytemplate> activities = new ArrayList<Activitytemplate>(dto.getActivities().size());

        for (ActivityTemplateDTO activity : dto.getActivities()) {
            Activitytemplate template = new Activitytemplate();
            template.setPkid(activity.getPkid());
            template.setSequenceno(activity.getSequenceNo());
            template.setDuedate(activity.getDueDate().intValue());
            template.setDuetime(activity.getDueTime().intValue());
            if (activity.getAlertTime() != null) {
                template.setAlerttime(activity.getAlertTime().intValue());
            }
            template.setSubmissiontype(activity.getSubmissionType().getCode());
            template.setDescription(activity.getDescription());
            template.setScheduletemplate(entity);
            activities.add(template);
        }

        entity.setActivities(activities);
    }

}
